<?php

namespace App\Http\Resources;

use Carbon\Carbon;
use Illuminate\Http\Resources\Json\JsonResource;

class SurveyResource extends JsonResource
{
    /**
     * @param \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $user = auth()->user();
        $count = $this->details()->count();
        return [
            'id' => $this->id,
            'name' => $this->name,
            'description' => $this->description,
            'questions' => json_decode($this->questions,true),
            'editor' => optional($this->editor)->realname,
            'can_edit' => $this->editor_id == $user->id && !$count,
            'can_delete' => $this->editor_id == $user->id && !$count,
            'created_at' => Carbon::parse($this->created_at)->format('Y-m-d H:i:s'),
            'updated_at' => Carbon::parse($this->updated_at)->format('Y-m-d H:i:s'),
        ];
    }
}
